from common.Ulitys import ForumDriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

from tests import ForumLogin

class ForumHome:
    driver=""
    url=""
    def __init__(self):
        self.driver=ForumDriver.driver
        # self.url="http://127.0.0.1:9580/index.html"
        # self.driver.get(self.url)
        
    def HomeTest(self):
        # 检查首页元素
        self.driver.find_element(By.CSS_SELECTOR,"#article_list_board_title")
        self.driver.find_element(By.CSS_SELECTOR,"#nav_board_index > a > span.nav-link-title")
        self.driver.find_element(By.CSS_SELECTOR,"#nav_board_index > a")
        self.driver.find_element(By.CSS_SELECTOR,"#topBoardList > li:nth-child(2) > a")
        self.driver.find_element(By.CSS_SELECTOR,"#topBoardList > li:nth-child(3) > a")
        # 检查第一个帖子是否存在，因为是动态加载，所以显示等待
        WebDriverWait(self.driver,5).until(
            EC.visibility_of_all_elements_located((By.CSS_SELECTOR,"#artical-items-body > div:nth-child(1)"))
        )
        self.driver.find_element(By.CSS_SELECTOR,"#artical-items-body > div:nth-child(1)")
        
        # 测试导航栏跳转 -> 跳到C++
        # 记录首页按钮 
        home_index=self.driver.find_element(By.CSS_SELECTOR,"#nav_board_index > a")
        # 点击C++模块，topBoardList > li:nth-child(3) > a
        self.driver.find_element(By.CSS_SELECTOR,"#topBoardList > li:nth-child(3) > a").click()
        # 检查page_title是否为C++
        time.sleep(1) # 等待页面加载
        text=self.driver.find_element(By.CSS_SELECTOR,"#article_list_board_title").text
        assert text=="C++"
        # 返回首页
        home_index.click()
        time.sleep(2)
        # 测试发布帖子按钮
        self.driver.find_element(By.CSS_SELECTOR,"#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post").click()
        # 检查是否跳转到发布帖子页面
        WebDriverWait(self.driver,5).until(
            EC.visibility_of_all_elements_located((By.CSS_SELECTOR,"#bit-forum-content > div.page-header.d-print-none > div > div > div > h2"))
        )
        text=self.driver.find_element(By.CSS_SELECTOR,"#bit-forum-content > div.page-header.d-print-none > div > div > div > h2").text
        print(text)
        assert text=="发新贴"
        
        home_index.click()
        # 点击帖子 - 显示等待
        WebDriverWait(self.driver,5).until(
            EC.visibility_of_all_elements_located((By.CSS_SELECTOR,"#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a"))
        )
        self.driver.find_element(By.CSS_SELECTOR,"#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a").click()
        # 检查是否跳转到帖子详情页面
        time.sleep(1)
        self.driver.find_element(By.CSS_SELECTOR,"#details_article_content_title")
        # 返回首页
        home_index.click()
        
        
        
        
        
        

